const fs = require('fs')
const path = require('path')
const MARK_FLAGS = {
    SV: 1 << 0,  //0b0001
    SA: 1 << 1,
    CV: 1 << 2,
    MA: 1 << 3
}

const TEMP_FILENAME = {
    SV: './temp_sccren_video.webm',
    SA: './temp_sys_audio.ogg',
    CV: './temp_camera_video.webm',
    MA: './temp_micor_audio.ogg'
}

function deleteTempFiles(tempPath) {
    fs.readdir(tempPath, (err, files) => {
        if (err) {
            console.error('Error reading temporary directory:', err);
            return;
        }
        files.forEach(file => {
            const filePath = path.resolve(tempPath, file);
            fs.unlink(filePath, (err) => {
                if (err) {
                    console.error('Error deleting file:', err);
                }
            });
        });
    });
}

// 写入文件
function writeFile(filePath, data) {
    return new Promise((resolve, reject) => {
        fs.writeFile(filePath, data, (err) => {
            if (err) {
                reject(err)
            } else {
                resolve('写入成功')
            }
        })
    })
}

// 保存临时文件
async function saveTempFile(data, fileName, filePath) {
    // const arrayBuffer = await data.arrayBuffer()
    // const buffer = Buffer.from(arrayBuffer)
    await writeFile(path.resolve(filePath, fileName), data)
}

// 转换时间格式
function timeConvart(date) {
    return `${date.getFullYear()}_${date.getMonth() + 1}_${date.getDate()} ${date.getHours()}_${date.getMinutes()}_${date.getSeconds()}_`

}
module.exports = { 
    MARK_FLAGS,
    TEMP_FILENAME,
    deleteTempFiles,
    saveTempFile,
    timeConvart
}